/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.felix.deployment.rp.autoconf;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.felix.metatype.AD;
import org.apache.felix.metatype.OCD;
import org.osgi.service.metatype.AttributeDefinition;
import org.osgi.service.metatype.ObjectClassDefinition;

public class ObjectClassDefinitionImpl implements ObjectClassDefinition {

   	private final OCD m_ocd;

	public ObjectClassDefinitionImpl(OCD ocd) {
		m_ocd = ocd;
	}
	
	public AttributeDefinition[] getAttributeDefinitions(int filter) {
		if (m_ocd.getAttributeDefinitions() == null) {
            return null;
        }
        if (filter != ObjectClassDefinition.OPTIONAL && filter != ObjectClassDefinition.REQUIRED && filter != ObjectClassDefinition.ALL) {
            return null;
        }

        List result = new ArrayList();
        for (Iterator i = m_ocd.getAttributeDefinitions().values().iterator(); i.hasNext();) {
        	AD ad = (AD) i.next();
        	if (filter != ObjectClassDefinition.ALL) {
        		if (ad.isRequired() && filter == ObjectClassDefinition.REQUIRED) {
        			result.add(new AttributeDefinitionImpl(ad));
        		}
        		else if (!ad.isRequired() && filter == ObjectClassDefinition.OPTIONAL) {
        			result.add(new AttributeDefinitionImpl(ad));
        		}
        	} else {
        		result.add(new AttributeDefinitionImpl(ad));
        	}
        }

        return (AttributeDefinition[]) result.toArray(new AttributeDefinition[result.size()]);
	}

	public InputStream getIcon(int size) throws IOException {
		return null;
	}

	public String getDescription() {
		return m_ocd.getDescription();
	}

	public String getID() {
		return m_ocd.getID();
	}

	public String getName() {
		return m_ocd.getName();
	}
}
